<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { equipApi } from '@/api/controller';
  import xmUpload from '@/components/Upload/xm-upload.vue';
  import xmImage from '@/components/xmImage/index.vue';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  const formLabelWidth = '160px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:''
  })
  interface formQuert {
    id:number
    mic_all_time:number
    min_rich_value:number
    max_rich_value:number
    min_charm_value:number
    max_charm_value:number
    min_user_rich_value:number
    max_user_rich_value:number
    max_user_recharge_value:number
    min_user_recharge_value:number
    after_day:number
    status:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    mic_all_time: [{ required: true, trigger: 'blur', message: '请输入主播在麦时长(分)' }],
    min_rich_value: [{ required: true, trigger: 'blur', message: '请输入主播最小财富值' }],
    max_rich_value: [{ required: true, trigger: 'blur', message: '请输入主播最大财富值' }],
    min_charm_value: [{ required: true, trigger: 'blur', message: '请输入主播最小魅力值' }],
    max_charm_value: [{ required: true, trigger: 'blur', message: '请输入主播最大魅力值' }],
    min_user_rich_value: [{ required: true, trigger: 'blur', message: '请输入用户最小财富值' }],
    max_user_rich_value: [{ required: true, trigger: 'blur', message: '请输入用户最大财富值' }],
    max_user_recharge_value: [{ required: true, trigger: 'blur', message: '请输入用户最大充值(元)' }],
    min_user_recharge_value: [{ required: true, trigger: 'blur', message: '请输入用户最小充值(元)' }],
    after_day: [{ required: true, trigger: 'blur', message: '请输入用户注册几天后可看' }],
    status: [{ required: true, trigger: 'blur', message: '请选择是否启用' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      await equipApi.change_chat_config(form)
      loading.close()
      ToastSuccess(`${action.title}成功`)
      $emit('updateList')
      closeDialog()
    })
  }
  const openDialog = (query:any = {})=> {
    const { id='', 
    mic_all_time='', min_rich_value='', max_rich_value='',min_charm_value='',
    max_charm_value='', min_user_rich_value='',max_user_rich_value='',max_user_recharge_value='',
    min_user_recharge_value='', after_day='', status=''
   } = query
    let formObj = {
      mic_all_time, min_rich_value, max_rich_value,min_charm_value,
      max_charm_value, min_user_rich_value,max_user_rich_value,max_user_recharge_value,
      min_user_recharge_value, after_day, status
    } as formQuert
    if(id) formObj.id = id
    Object.assign(form,formObj)
    action.title = id?'新增公聊准入配置':'编辑公聊准入配置'
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="60%"
    :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-row>
          <el-col :span="12">
            <el-form-item :label-width="formLabelWidth" label="主播在麦时长(分)" prop="mic_all_time">
              <el-input type="number" v-model="form.mic_all_time" placeholder="请输入主播在麦时长(分)"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="主播最小财富值" prop="min_rich_value">
              <el-input type="number" v-model="form.min_rich_value" placeholder="请输入主播最小财富值"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="主播最大财富值" prop="max_rich_value">
              <el-input type="number" v-model="form.max_rich_value" placeholder="请输入主播最大财富值"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="主播最小魅力值" prop="min_charm_value">
              <el-input type="number" v-model="form.min_charm_value" placeholder="请输入主播最小魅力值"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="主播最大魅力值" prop="max_charm_value">
              <el-input type="number" v-model="form.max_charm_value" placeholder="请输入主播最大魅力值"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="是否启用" prop="status">
              <el-select v-model="form.status" placeholder="请选择是否启用">
                <el-option label="启用" :value="1" />
                <el-option label="关闭" :value="2" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label-width="formLabelWidth" label="用户最小财富值" prop="min_user_rich_value">
              <el-input type="number" v-model="form.min_user_rich_value" placeholder="请输入用户最小财富值"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="用户最大财富值" prop="max_user_rich_value">
              <el-input type="number" v-model="form.max_user_rich_value" placeholder="请输入用户最大财富值"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="用户最小充值元" prop="min_user_recharge_value">
              <el-input type="number" v-model="form.min_user_recharge_value" placeholder="请输入用户最小充值(元)"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="用户最大充值元" prop="max_user_recharge_value">
              <el-input type="number" v-model="form.max_user_recharge_value" placeholder="请输入用户最大充值(元)"/>
            </el-form-item>
            <el-form-item :label-width="formLabelWidth" label="用户注册几天后" prop="after_day">
              <el-input type="number" v-model="form.after_day" placeholder="请输入用户注册几天后"/>
            </el-form-item>
          </el-col>
        </el-row>
        
      </el-form>

      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>